package com.juzipi.demo.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.juzipi.demo.model.PageBody;
import com.juzipi.demo.pojo.Student;
import com.juzipi.demo.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author juzipi
 * @Date 2021/5/3 15:30
 * @Info
 */
@Service
public class StudentPlusService {


    @Autowired
    private StudentRepository studentRepository;
    @Autowired
    private MongoTemplate mongoTemplate;


    @Cacheable(value = "student",key = "queryById")
    public Student queryById(Long id) {
        //不get的话是一个option对象
        return studentRepository.findById(String.valueOf(id)).get();
    }


    public List<Student> queryList() {
        return studentRepository.findAll();
    }

    public List<Student> queryListByName(Student student) {
        //使用封装方法反而感觉难了，估计是因为陌生的原因,这个就离谱，传给他一个student对象它会自己匹配里面字段来做一个条件查询
        //看到一个简单的方法就是直接在 MongoRepository 里加方法，它会根据方法名自动识别
        List<Student> students = studentRepository.findAll(Example.of(student));
        System.out.println(students);
        return students;
    }

    public Student queryByName(String name){
        return studentRepository.findByName(name);
    }

    public Map<String, Object> queryPageByLike(String key, Integer pageNum, Integer pageSize) {
        PageRequest pageRequest = PageRequest.of(pageNum, pageSize);
        //模糊查询条件，难写还不好用，弃了，pagehelper吊打这个分页查询

//        ExampleMatcher exampleMatcher = ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING);
//        Example<Student> example = Example.of(new Student(),exampleMatcher);
//        Page<Student> students = studentRepository.findAll(example,pageRequest);

        PageHelper.startPage(pageNum, pageSize);
        List<Student> students = studentRepository.findByNameLike(key);

        PageInfo<Student> studentPageInfo = new PageInfo<>(students);

        LinkedHashMap<String, Object> linkedHashMap = new LinkedHashMap<>();
        linkedHashMap.put("当前页: ",studentPageInfo.getPageNum());
        linkedHashMap.put("页面大小: ", studentPageInfo.getSize());
        linkedHashMap.put("总页数: ", studentPageInfo.getPages());
        linkedHashMap.put("总条数: ", studentPageInfo.getTotal());
        linkedHashMap.put("数据集: ",studentPageInfo.getList());
        linkedHashMap.put("最后一条？",studentPageInfo.getEndRow());
        System.out.println(studentPageInfo.getList());
        System.out.println("==============>>>>>>>>>"+studentPageInfo);
        return linkedHashMap;
    }

    public Boolean insert(Student student) {
        Student save = studentRepository.save(student);
        return !StringUtils.isEmpty(save);
    }

    public Boolean update(Student student) {
        //修改和新增一样的方法，区别是修改时要传id，它会根据id进行修改，没有id就是新增了
        Student save = studentRepository.save(student);
        return !StringUtils.isEmpty(save);
    }

    public Boolean delete(String id) {
        studentRepository.deleteById(id);
        return true;
    }

    public List<Student> queryListByAge(Integer age) {
        Query query = new Query(Criteria.where("age").is(age));
        return mongoTemplate.find(query, Student.class);
    }

    public void queryPageLikePlus(PageBody pageBody) {

        studentRepository.


    }


}
